package com.example.album;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpSession;

@Service
public class AlbumManagementService {
    @Autowired
    private AlbumRepository ar;
    @Autowired
    private CategoryRepository cr;
    @Autowired
    private ImgRepository ir;
    @Autowired
    private UserRepository ur;
    @Autowired
    private CollectionRepository cor;
    @Autowired
    private HttpSession session;
    public Album createAlbum(String userid, String albumName, String categoryname) {
        Album album = new Album();
        long count = ar.count();
        String newAlbumId = String.valueOf(count + 1);
        String categoryid = cr.findByCategoryName(categoryname).getCategoryId();
        album.setOwnerId(userid);
        album.setAlbumName(albumName);
        album.setCategoryId(categoryid);
        album.setAlbumId(newAlbumId);
        album.setCountCollection(0);
        album.setCountLike(0);
        album.setCountComment(0);
        album.setStatus(0); // 假设0表示正常状态
        album.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return ar.save(album);
    }
    public List<String> getAllCategories() {
        List<Category> categories = cr.findAll();
        List<String> result = new java.util.ArrayList<>();
        for (int i = 0; i < categories.size(); i++) {
            result.add(categories.get(i).getCategoryName());
        }
        return result;
    }
    public HashMap<String, Object> getAlbumInfo(String albumId) {
    HashMap<String, Object> result = new HashMap<>();
    Album album = ar.findByAlbumId(albumId);

    result.put("title", album.getAlbumName());
    result.put("author", ur.findByUserId(album.getOwnerId()).getUsername());
    result.put("authorId", album.getOwnerId());
    result.put("created", album.getCreateTime().toString());
    result.put("category", cr.findByCategoryId(album.getCategoryId()).getCategoryName());
    List<Img> imgs = ir.findByAlbumId(albumId);
    // 只加载status!=1的图片
    List<HashMap<String, String>> photos = imgs.stream()
        .filter(img -> img.getStatus() == null || img.getStatus() != 1)
        .map(img -> {
            HashMap<String, String> m = new HashMap<>();
            m.put("photoId", img.getPhotoId());
            m.put("fileUrl", img.getFileUrl());
            return m;
        }).collect(Collectors.toList());
    System.out.println("Photos: " + photos);
    result.put("photos", photos);
    return result;
    }

    public void renameAlbum(String albumId, String newTitle) {
        Album album = ar.findByAlbumId(albumId);
        if (album != null) {
            album.setAlbumName(newTitle);
            ar.save(album);
        }
    }

    public void changeAlbumCategory(String albumId, String newCategory) {
        Album album = ar.findByAlbumId(albumId);
        Category category = cr.findByCategoryName(newCategory);
        if (album != null && category != null) {
            album.setCategoryId(category.getCategoryId());
            ar.save(album);
        }
    }
    public void deleteAlbum(String albumId) {
        Album album = ar.findByAlbumId(albumId);
        if (album != null) {
            album.setStatus(1); // 假设1表示已删除
            ar.save(album);
        }
    }

    public void setAlbumCover(String albumId, String photoId) {
        Album album = ar.findByAlbumId(albumId);
        if (album != null) {
            album.setCoverId(photoId);
            ar.save(album);
        }
    }

    public List<Map<String, String>> getAllAlbumsBrief() {
        List<Album> albums = ar.findByStatusNot(1); // 排除已删除的相册
        List<Map<String, String>> result = albums.stream().map(album -> {
            Img coverImg = null;
            if (album.getCoverId() != null) {
                coverImg = ir.findByPhotoId(album.getCoverId());
            }
            Map<String, String> m = new HashMap<>();
            m.put("albumId", album.getAlbumId());
            m.put("title", album.getAlbumName());
            m.put("author", ur.findByUserId(album.getOwnerId()).getUsername());
            m.put("category", cr.findByCategoryId(album.getCategoryId()).getCategoryName());
            m.put("coverUrl", coverImg != null ? coverImg.getFileUrl() : null);
            return m;
        }).collect(Collectors.toList());
        return result;
    }

    public List<Map<String, String>> getMyAlbumsBrief() {
        String userId = (String) session.getAttribute("userid");
        List<Album> albums = ar.findByOwnerIdAndStatusNot(userId, 1); // 排除已删除的相册
        List<Map<String, String>> result = albums.stream().map(album -> {
            Img coverImg = null;
            if (album.getCoverId() != null) {
                coverImg = ir.findByPhotoId(album.getCoverId());
            }
            Map<String, String> m = new HashMap<>();
            m.put("albumId", album.getAlbumId());
            m.put("title", album.getAlbumName());
            m.put("author", ur.findByUserId(album.getOwnerId()).getUsername());
            m.put("category", cr.findByCategoryId(album.getCategoryId()).getCategoryName());
            m.put("coverUrl", coverImg != null ? coverImg.getFileUrl() : null);
            return m;
        }).collect(Collectors.toList());
        return result;
    }

    public List<Map<String, String>> getCollectedAlbumsBrief(String userId) {
        List<String> collectedAlbumIds = cor.findAlbumIdsByUserId(userId);
        List<Album> albums = ar.findByAlbumIdIn(collectedAlbumIds);
        List<Map<String, String>> result = albums.stream().map(album -> {
            Img coverImg = null;
            if (album.getCoverId() != null) {
                coverImg = ir.findByPhotoId(album.getCoverId());
            }
            Map<String, String> m = new HashMap<>();
            m.put("albumId", album.getAlbumId());
            m.put("title", album.getAlbumName());
            m.put("author", ur.findByUserId(album.getOwnerId()).getUsername());
            m.put("category", cr.findByCategoryId(album.getCategoryId()).getCategoryName());
            m.put("coverUrl", coverImg != null ? coverImg.getFileUrl() : null);
            return m;
        }).collect(Collectors.toList());
        return result;
    }
}
